<template>
  <van-popup round v-model="show_sub_add" @close="close_pop">
    <div class="form">
      <div class="title">
        <div class="c_flex" style="padding:10px">添加任务</div>
      </div>
      <van-form :label-width="60" validate-trigger="onSubmit" validate-first @submit="onSubmit">


        <dept :options="dept_op.options" :fieldNames="dept_op.fieldNames" v-model="user_data.department_id"
          :is_array="false" txt_tip="部门选择" />
        <dept :options="job_op.options" :fieldNames="job_op.fieldNames" v-model="user_data.jobs" :is_array="true"
          txt_tip="职位" />
        <dept :options="role_op.options" :fieldNames="role_op.fieldNames" v-model="user_data.roles" :is_array="true"
          txt_tip="角色" />
        <normal_item v-model="user_data.email" :to_rule="[{pattern:email_pattern,message:' 请符合邮箱格式'}]" txt_tip="邮箱" />
        <normal_item v-model="user_data.mobile" :to_rule="[{pattern:phone_pattern,message:' 请填写正确格式的手机号'}]" txt_tip="手机号" />
        <normal_item v-model="user_data.name" :to_rule="[{pattern:name_pattern,message:' 请填写正确的汉字姓名'}]" txt_tip="真实姓名" />
        <normal_item v-model="user_data.password" :to_rule="[{pattern:password_pattern,message:' 密码最低5位，最高12位'}]"
          txt_tip="密码" />

        <div class="submit_out c_flex">
          <van-button native-type="submit" class="submit">确定</van-button>
        </div>
      </van-form>
    </div>
  </van-popup>
</template>

<script>
  import {
    UserService,
    DeptService,
    JobService,
    RoleService,
  } from '@/api';
  import dept from "./component/dept.vue"
  import normal_item from './component/normal_item.vue';
  export default {
    name: 'add_user',
    mounted() {
      DeptService.getList().then(res => {
        // console.log(res)
        this.dept_op.options = res.data;

      });
      JobService.getList({
        paginate: 0
      }).then(res1 => {
        console.log(res1)
        this.job_op.options = res1.data;
      })
      RoleService.getList().then(res1 => {
        console.log('res2', res1)
        this.role_op.options = res1.data;
      })
    },
    props: {
      show_add: Boolean,
    },
    components: {
      dept,
      normal_item
    },
    computed: {

      show_sub_add: {
        get() {
          return this.show_add
        },
        set(value) {
          console.log(value);
          if (!value) {
            this.close_pop();
          }
        }
      }
    },

    data() {
      return {
        user_data: {
          department_id: '',
          jobs: '',
          roles: '',
          email: '',
          phone: '',
          password: '',
          name: '',
        },
        email_pattern: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
        phone_pattern: /^(?:(?:\+|00)86)?1\d{10}$/,
        name_pattern: /^(?:[\u4e00-\u9fa5·]{2,16})$/ ,
        password_pattern: /^[a-zA-Z0-9]{6,}$/,
        dept_op: {
          options: [],
          fieldNames: {
            text: 'department_name',
            value: 'id',
            children: 'children',
          }
        },
        job_op: {
          options: [],
          fieldNames: {
            text: 'job_name',
            value: 'id',
            children: 'children',
          }
        },
        role_op: {
          options: [],
          fieldNames: {
            text: 'role_name',
            value: 'id',
            children: 'children',
          }
        },

      }
    },
    watch: {

    },
    methods: {


      onSubmit() {
        
        UserService.addUser(this.user_data).then(res => {
          if(res.code == 10000){
            this.$EventBus.$emit('update_user_list');
            this.close_pop();
          }
        });
      },
      close_pop() {
        this.empty();
        this.$emit('update:show_add', false);
      },
      empty() {
        this.user_data = this.$options.data().user_data;
      }
    },
  }
</script>

<style lang="less" scoped>
  .icon_size {
    width: 30px;
    height: 30px;
    margin-right: 15px;
  }

  .form {
    .title {
      width: 50%;
      border-bottom: 1px solid rgba(153, 153, 153, 1);
      margin: 0 auto 15px;
    }

    width:540px;


    .submit_out {
      margin-top: 40px;
      margin-bottom: 30px;
    }

    .submit {

      width: 360px;
      height: 70px;
      border-radius: 37.84px;
      background-color: rgba(235, 95, 17, 1);
      letter-spacing: 1.8px;
      font-size: 28px;
      color: rgba(255, 255, 255, 1);
    }
  }
</style>